package com.xidian.zhiancloud.controller.riskcontrol.risksystem;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.Page;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.controller.checkdanger.ExcelReadListener;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.enums.UserResponseEnums;
import com.xidian.zhiancloud.model.riskcontrol.TWarSignCategoryStatusList;
import com.xidian.zhiancloud.model.riskcontrol.entity.TSignCategoryStaEntity;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.riskcontrol.risksystem.TWarSignCategoryStatusService;
import com.xidian.zhiancloud.util.api.ExcelUtil2;
import com.xidian.zhiancloud.util.tools.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风险管控-系统设置-警示标志类别设置
 */
@RestController
@RequestMapping("/twarsigncategory")
public class TWarSignCategoryStatusController extends BaseController {

    @Autowired
    TWarSignCategoryStatusService tWarSignCategoryStatusService;

    /**
     * 系统设置-警示标志类别设置列表
     * @param reqdata 多对象集合
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getTWarSignCategoryStatusList")
    public ResultDTO getTWarSignCategoryStatusList(BaseReqQuery reqdata){
        Long companyId=this.getSessionUserCompanyId();
        Page<TWarSignCategoryStatusList> pagedata =
                tWarSignCategoryStatusService.getTWarSignCategoryStatusList
                        (reqdata,companyId);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("datalist", pagedata);
        data.put("PageNum", pagedata.getPageNum());
        data.put("PageSize", pagedata.getPageSize());
        data.put("PageTotal", pagedata.getPages());
        data.put("total", pagedata.getTotal());
        return ResultDTO.successOf(data, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 系统设置-警示标志类别设置添加
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/addTWarSignCategoryStatus")
    public ResultDTO addTWarSignCategoryStatus(
         TWarSignCategoryStatusList tWarSignCategorySta){
        Long companyId=this.getSessionUserCompanyId();
        Long userId=this.getSessionUserid();
        tWarSignCategorySta.setCompanyId(companyId);
        tWarSignCategorySta.setPersonAdd(String.valueOf(userId));
        tWarSignCategoryStatusService.addTWarSignCategoryStatus
                (tWarSignCategorySta);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 系统设置-警示标志类别设置修改
     *  @param id 警示标志类别设置列表ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/updateTWarSignCategoryStatus")
    public ResultDTO updateTWarSignCategoryStatus(@RequestParam("id") String id,
        TWarSignCategoryStatusList tWarSignCategorySta) {
        tWarSignCategoryStatusService.updateTWarSignCategoryStatus
                (id,tWarSignCategorySta);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 系统设置-警示标志类别设置删除
     * @param id 风险点表ID
     *
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/deleteTWarSignCategoryStatus")
    public ResultDTO deleteTWarSignCategoryStatus(@RequestParam("id") String id) {
        tWarSignCategoryStatusService.deleteTWarSignCategoryStatus(id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     *  -导出警示标志类别设置
     * @param id 警示标志类别设置ID
     */
    @GetMapping("/exportTSignCategoryStaData")
    public void exportTSignCategoryStaData (HttpServletResponse response,
         @RequestParam("id") String id) throws IOException {
        Long companyId=this.getSessionUserCompanyId();
        Page<TWarSignCategoryStatusList> pagedata = tWarSignCategoryStatusService
                .getExportTSignCategoryStaData(companyId,id);
        List<TWarSignCategoryStatusList> list = pagedata.getResult();
        ExcelUtil2<TWarSignCategoryStatusList> util = new
                ExcelUtil2<TWarSignCategoryStatusList>(TWarSignCategoryStatusList.class);
        util.exportExcel(response, list, "警示标志类别设置");
    }

    /**
     * 导入警示标志类别设置
     * @param file 文件
     * @return
     */
    @RequestMapping("/importTSignCatStaData")
    @ResponseBody
    public ResultDTO importTSignCatStaData(@RequestParam MultipartFile file){
        Long companyId=this.getSessionUserCompanyId();
        Long userId=this.getSessionUserid();
        try{
            Object obj = TSignCategoryStaEntity.class.newInstance();
            List<Object> list = ExcelUtils.importBaseExcel0
                    (file,"警示标志类别设置",obj);
                for(int i=0;i<list.size();i++) {
                    TSignCategoryStaEntity signCatSta =
                            (TSignCategoryStaEntity)list.get(i);
                    tWarSignCategoryStatusService.addTSignCategoryStaEntity
                            (companyId,userId,signCatSta);
                }
        }catch (Exception e){
            e.printStackTrace();
            return ResultDTO.errorOf(UserResponseEnums.DATA_IMPORT_FAILED_ERROR);
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    @RequestMapping("/ImportExcelFile")
    public ResultDTO ImportExcelFile(@RequestParam("file") MultipartFile file) throws IOException{

        ExcelReadListener excelistener = new ExcelReadListener();
        //excelistener.userservice = userservice;
        String sfile = "D:\\桌面文件\\警示标志 导入模板.xlsx";

        EasyExcel.read(file.getInputStream(),  excelistener).sheet(0).doRead();

        return ResultDTO.successOf(null, GeneralResponseEnums.SUCCESS);
    }

}
